package com.handlemvc.springmvc.context.xml;

import com.handlemvc.springmvc.util.ClassUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * @author ：tiger.
 * @date ：2021/10/13 01:15
 */
public class XmlParser {

	public static String getComponetScanBasePackage(String xmlPath){
		Element element = parseElement(xmlPath);
		if(element == null){
			return "";
		}
		return element.element("component-scan").attributeValue("basepackage");
	}

	public static Element parseElement(String xmlPath) {
		SAXReader saxReader = new SAXReader();
		try {
			Document document = saxReader.read(ClassUtils.getClassLoader().getResourceAsStream(xmlPath));
			return document.getRootElement();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return null;
	}


	public static Element getElement(Element element,String elementName){
		return element.element(elementName);
	}

	public static String getAttribute(Element element,String attributeName){
		return element.attributeValue(attributeName);
	}
}
